<template>
	<view v-if="shenheStatus == 0" class="page">
		<u-sticky :h5-nav-height="0">
			<!-- @change="change" -->
			<pageHeader ref="pageHeader" :params="{
          cate,
          cat_id,
          sortList,
          sort,
          time_type,
          distance_id,
          distance,
        }"></pageHeader>
		</u-sticky>

		<pageList :list="list"></pageList>

		<toolsMore :status="moreStatus"></toolsMore>
	</view>
	<Shenhe v-else-if="shenheStatus == 1"></Shenhe>
</template>

<script>
	import Shenhe from "@/components/shenhe/shenhe";
	import toolsMore from "@/components/tools/tools-more.vue";
	import pageHeader from "./components/page-header.vue";
	import pageList from "./components/page-list.vue";
	import {
		workerList,
		workerIndex
	} from "@/api/marketing/callOut";

	export default {
		components: {
			Shenhe,
			toolsMore,
			pageHeader,
			pageList,
		},

		data() {
			return {
				list: [],
				moreStatus: 1,
				page: 1,
				isEnd: false,
				name: "",
				cate: [],
				cat_id: "",

				sortList: [
					{
						name: "距离优先",
						id: ""
					},{
						name: "智能排序",
						id: "1"
					},
					
				],
				sort: "",

				time_type: "",

				distance: "0",
				distance_id: "",

				longitude: "",
				latitude: "",
			};
		},

		async onLoad() {
			this.getLocation();
		},

		onShow() {
			this.Emit.$on("handle", this.handle);
		},

		onHide() {
			this.Emit.$off("handle");
		},

		onUnload() {
			this.Emit.$off("handle");
		},

		methods: {
			async getLocation() {
				try {
					let res = await this.$store.dispatch("getLocation");
					this.longitude = res.longitude;
					this.latitude = res.latitude;

					this.workerIndex();
					this.clearData();
				} catch (error) {
					this.workerIndex();
					this.clearData();
				}
			},

			async workerIndex() {
				try {
					const res =await this.$allrequest.mch_store.mchFen({})
					if (!res.code) {
						this.cate = res.data.list || [];
					}
				} catch (err) {
					this.$utils.toast(err);
				}
			},

			handle(type, params) {
				switch (type) {
					case "openDistance":
						this.$refs.pageHeader.distanceIndex = "";
						this.distance_id = "";
						this.distance = "0";
						this.clearData();
						break;

					default:
						if (type === "distance_id") {
							this.$refs.pageHeader.distanceIndex = 1;
							this.distance = "";
						}

						if (this[type] === params) {
							this[type] = "";
						} else {
							this[type] = params;
						}

						this.$refs.pageHeader.showType = false;
						this.$refs.pageHeader.searchValue = "";
						this.clearData();
						break;
				}
			},

			clearData() {
				this.page = 1;
				this.isEnd = false;
				this.list = [];
				this.getList();
			},

			async getList() {
				this.moreStatus = 2;
				console.log('this.sort',this.sort)
				let orderByDis1
				let orderByAi1
				if(this.sort==''){
					orderByAi1=0
					orderByDis1=1
				}else{
					orderByDis1=0
					orderByAi1=1
				}
				let prives;
				const distanceMap = {
				  '0': 0,
				  '500m': 500,
				  '1km': 1000,
				  '3km': 3000,
				  '5km': 5000,
				  '10km': 10000
				};
				prives = distanceMap[this.distance] !== undefined ? distanceMap[this.distance] : null;
				const res =await this.$allrequest.mch_store.mchList({
					is_open:1,
					mch_common_cat_id:this.cat_id,
					name:this.name,
					dis:distanceMap[this.distance],
					// 距离优先
					orderByDis:orderByDis1,
					// 智能排序
					orderByAi:orderByAi1,
					lng:this.longitude,
					lat:this.latitude,
					page:this.page,
					
				});
				uni.stopPullDownRefresh();
				this.moreStatus = 1;

				if (res.code == 0) {
					this.list.push(... res.data.list);
					console.log('我是列表数据',this.list)
					this.page = this.page + 1;
					if (this.list.length>=res.data.totalCount) {
						this.isEnd = true;
						this.moreStatus = 3;
					}
				}
			},
		},

		onReachBottom() {
			if (this.isEnd) {
				return;
			}

			this.getList();
		},

		onPullDownRefresh() {
			this.getLocation();
		},
	};
</script>

<style lang="scss">
	.page {
		min-height: 100vh;
		background: #f2f2f2;
		overflow: hidden;
	}
</style>